<?php
include('../resource/db/db.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']) && isset($_POST['user_id'])) {
    $name = $_POST['name'];
    $info = $_POST['info'];
    $user_id = intval($_POST['user_id']);

    $name = mysqli_real_escape_string($conn, $name);
    $info = mysqli_real_escape_string($conn, $info);

    // 检查是否存在同名的卡组
    $check_sql = "SELECT set_id FROM sets WHERE user_id='$user_id' AND name='$name' AND is_deleted=FALSE";
    $check_result = $conn->query($check_sql);
    if ($check_result->num_rows > 0) {
        echo "已经有相同名称的卡组";
        exit();
    }

    // 创建新的卡组
    $sql = "INSERT INTO sets (user_id, name, info, created_at, is_deleted, reward, is_copy) VALUES ('$user_id', '$name', '$info', NOW(), 0, 0, 0)";
    if ($conn->query($sql) === TRUE) {
        echo $conn->insert_id;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
